//
//  ServeViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/13.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "ServeViewController.h"

#import "ServeTableViewCell.h"
#import "ServeWebViewController.h"

@interface ServeViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

//轮播图
@property (nonatomic, strong) UIScrollView * lunbo_scrollView;
@property (nonatomic, strong) UIPageControl * lunbo_pageControl;
@property (nonatomic, strong) NSTimer * lunbo_timer;
@property (nonatomic, assign) BOOL isplay;

//轮播图
@property (nonatomic, strong) UIImageView * image1;
@property (nonatomic, strong) UIImageView * image2;
@property (nonatomic, strong) UIImageView * image4;
//数据
@property (nonatomic, strong) NSMutableArray * arr_lunboData;

@end

@implementation ServeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];

    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"智顺服务"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    _btnRight.hidden = YES;

    _btnLeft.hidden = YES;
}

//显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[ServeTableViewCell class] forCellReuseIdentifier:@"cell_server"];
    
    //刷新
    __unsafe_unretained UITableView * tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data];
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
//     //马上进入刷新状态
//    [tableView.mj_header beginRefreshing];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉加载
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{

        // 结束刷新
        [tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 数据
- (void )p_data
{
    self.arr_lunboData = @[@"banner2.png",@"banner5.png",@"banner.png",@"banner4.png",@"banner3.png"].mutableCopy;
    [self p_lunbotu];

    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - tableView代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + 5;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        if(SCREEN_WIDTH == 375)
        {
            return 180;
        }
        else if(SCREEN_WIDTH == 320)
        {
            return 150;
        }
        else
        {
            return 200;
        }
    }
    else
    {
        return 80;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.row == 0)
    {
//        cell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self p_lunbotu];
        
        [cell addSubview:self.lunbo_scrollView];
        
        [cell addSubview:self.lunbo_pageControl];
        
    }
    else
    {
        ServeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_server" forIndexPath:indexPath];
        
        switch (indexPath.row)
        {
            case 1:
            {
                cell.name.text = @"网站建设";
                cell.detail.text = @"智顺网络专注于高品质网站建设,提供精心策划、一流设计及专业的技术开发服务";
                cell.image.image = [UIImage imageNamed:@"website.png"];
            }
                break;
            case 2:
            {
                cell.name.text = @"SEO优化";
                cell.detail.text = @"智顺网络PPD系统实行按日付费推广,在投入成本最小化的同时实现客户利益的最大化";
                cell.image.image = [UIImage imageNamed:@"SEO.png"];
            }
                break;
            case 3:
            {
                cell.name.text = @"纷享销客";
                cell.detail.text = @"企业一站式工作平台,创新型颠覆OA、CRM等传统办公软件,让工作人群畅享3A办公新方式";
                cell.image.image = [UIImage imageNamed:@"fenxiang.png"];
            }
                break;
            case 4:
            {
                cell.name.text = @"APP开发";
                cell.detail.text = @"专业的研发团队全程保驾护航,丰富的APP开发经验,致力于专业的APP开发定制服务";
                cell.image.image = [UIImage imageNamed:@"APP.png"];
            }
                break;
            case 5:
            {
                cell.name.text = @"400服务";
                cell.detail.text = @"全国统一400电话接入号，全面展示现代企业品牌形象，是营销和客户服务的好帮手";
                cell.image.image = [UIImage imageNamed:@"400.png"];
            }
                break;
                
            default:
                break;
        }
        
        return cell;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ServeWebViewController * serveWebViewController = [[ServeWebViewController alloc] init];
    
    switch (indexPath.row)
    {
        case 1:
        {
            serveWebViewController.str_title = @"网站建设";
        }
            break;
        case 2:
        {
            serveWebViewController.str_title = @"SEO优化";
        }
            break;
        case 3:
        {
            serveWebViewController.str_title = @"纷享销客";
        }
            break;
        case 4:
        {
            serveWebViewController.str_title = @"APP开发";
        }
            break;
        case 5:
        {
            serveWebViewController.str_title = @"400服务";
        }
            break;
        default:
            break;
    }

    [self showViewController:serveWebViewController sender:nil];
}

#pragma mark - 轮播图
- (void)p_lunbotu
{
    self.arr_lunboData = @[@"banner2.png",@"banner5.png",@"banner.png",@"banner4.png",@"banner3.png"].mutableCopy;
    
    if(SCREEN_WIDTH == 375)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
    }
    else if(SCREEN_WIDTH == 320)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    }
    else
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    self.lunbo_scrollView.pagingEnabled = YES;
    self.lunbo_scrollView.showsHorizontalScrollIndicator = NO;
    self.lunbo_scrollView.delegate = self;
    self.lunbo_scrollView.userInteractionEnabled = YES;
    
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.arr_lunboData.count + 2), 0);
    
    if(self.arr_lunboData.count == 0)
    {
        self.image4.hidden = NO;
        self.lunbo_scrollView.scrollEnabled = NO;
        
        self.image4 = [[UIImageView alloc] init];
        self.image4.frame = CGRectMake(SCREEN_WIDTH * 1, 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
        [self.image4 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"placeholder_short.jpg"]];
        [self.lunbo_scrollView addSubview:self.image4];
        
        
        self.image1 = [[UIImageView alloc] init];
        self.image1.frame = CGRectMake(SCREEN_WIDTH * 0 , 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
        [self.image1 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"placeholder_short.jpg"]];
        [self.lunbo_scrollView addSubview:self.image1];
    }
    else
    {
        [self lunsdsd];
        
        self.lunbo_pageControl = [[UIPageControl alloc] init];
        
        if(SCREEN_WIDTH == 320)
        {
            self.lunbo_pageControl.frame = CGRectMake(0, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        else if(SCREEN_WIDTH == 375)
        {
            self.lunbo_pageControl.frame = CGRectMake(- 27.5, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        else
        {
            self.lunbo_pageControl.frame = CGRectMake(- 47, self.lunbo_scrollView.frame.size.height - 20, SCREEN_WIDTH, 18);
        }
        //    self.lunbo_pageControl.backgroundColor = [UIColor orangeColor];
        self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
        self.lunbo_pageControl.currentPageIndicatorTintColor = navi_bar_bg_color;
        self.lunbo_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:202/255.0 green:218/255.0 blue:233/255.0 alpha:1];
    }
    
    //手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.lunbo_scrollView addGestureRecognizer:tapGesture];
}

#pragma mark - 轮播图的点击事件
- (void)tapGesture:(id)sender
{
    NSLog(@"%ld",self.lunbo_pageControl.currentPage);
    
    
}

#pragma mark - 定时器的方法!
- (void)timerAction
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    int count = x / SCREEN_WIDTH;
    
    if(count < self.arr_lunboData.count)
    {
        count ++;
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
            
        }];
    }
    else if(count == self.arr_lunboData.count)
    {
        count ++;
        
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = 0;
            
        }];
    }
    else
    {
        count = 2;
        self.lunbo_scrollView.contentOffset = CGPointMake(1 * SCREEN_WIDTH, 0);
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
        }];
    }
}
#pragma mark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    
    int count = x / SCREEN_WIDTH;
    
    if(count == self.arr_lunboData.count + 1)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = 0;
    }
    else if(count == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(self.arr_lunboData.count * SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = self.arr_lunboData.count - 1;
    }
    else
    {
        self.lunbo_pageControl.currentPage = count - 1;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if([self.lunbo_scrollView isEqual:scrollView])
    {
        [self.lunbo_timer invalidate];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([scrollView isEqual:self.lunbo_scrollView]) {
        
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
}

- (void)lunsdsd
{
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.arr_lunboData.count + 2), 0);
    
    self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
    
    self.lunbo_scrollView.scrollEnabled = YES;
    
    self.image4.hidden = YES;
    
    for (int i = 1; i <= self.arr_lunboData.count; i++)
    {
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height)];
        
        image.image = [UIImage imageNamed:self.arr_lunboData[i - 1]];
        
        [self.lunbo_scrollView addSubview:image];
    }
    
    self.image1 = [[UIImageView alloc] init];
    self.image1.frame = CGRectMake(SCREEN_WIDTH * 0 , 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
    self.image1.image = [UIImage imageNamed:self.arr_lunboData.lastObject];
    [self.lunbo_scrollView addSubview:self.image1];
    
    self.image2 = [[UIImageView alloc] init];
    self.image2.frame = CGRectMake(SCREEN_WIDTH * (self.arr_lunboData.count + 1), 0 , SCREEN_WIDTH, self.lunbo_scrollView.frame.size.height);
    self.image2.image = [UIImage imageNamed:self.arr_lunboData.firstObject];
    [self.lunbo_scrollView addSubview:self.image2];
    
    
    if(self.isplay == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播秒数
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        self.lunbo_pageControl.currentPage = 0;
        
        self.isplay = 1;
    }
    else
    {
//        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (self.lunbo_pageControl.currentPage + 1), 0);
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
}



@end
